package presentation;

import java.awt.Color;
import java.awt.Font;
import java.text.DecimalFormat;
import java.util.Observable;
import java.util.Observer;

import org.jfree.chart.plot.dial.DialBackground;
import org.jfree.chart.plot.dial.DialCap;
import org.jfree.chart.plot.dial.DialPlot;
import org.jfree.chart.plot.dial.DialPointer;
import org.jfree.chart.plot.dial.DialTextAnnotation;
import org.jfree.chart.plot.dial.DialValueIndicator;
import org.jfree.chart.plot.dial.StandardDialFrame;
import org.jfree.chart.plot.dial.StandardDialScale;
import org.jfree.data.general.DefaultValueDataset;
import org.jfree.ui.RectangleAnchor;
import org.jfree.ui.TextAnchor;

public class PSpeedDial extends DialPlot implements Observer {
	private static final long serialVersionUID = 1L;
	private DefaultValueDataset data;
	
	public PSpeedDial(double[] boundary) {
		if (boundary[0] == 0 && boundary[1] == 0) {
			boundary[0] = 0;
			boundary[1] = 200;
		}
		
		Double steps = 20.;
		
		/** The dataset. */
		data = new DefaultValueDataset(0);
		this.setDataset(data);
		
		// Dial title
		DialTextAnnotation title = new DialTextAnnotation("km/h");
        title.setAngle(90);
        title.setFont(new Font("Arial", Font.BOLD, 30));
        title.setRadius(0.50);
        title.setPaint(Color.lightGray);
        this.addLayer(title);
        
        // Tickmarks and text
        StandardDialScale scale = new StandardDialScale(boundary[0], boundary[1], 210, -240, steps, 4);
        scale.setTickRadius(0.93);
        scale.setTickLabelOffset(0.18);
        scale.setTickLabelFont(new Font("Arial", Font.PLAIN, 14));
        scale.setTickLabelPaint(Color.black);
        scale.setTickLabelFormatter(new DecimalFormat("###"));
        this.addScale(0, scale);
        
        // Frame
        StandardDialFrame dialFrame = new StandardDialFrame();
        dialFrame.setBackgroundPaint(Color.lightGray);
        dialFrame.setForegroundPaint(Color.darkGray);
        this.setDialFrame(dialFrame);
        
        //Needle attachment
		DialCap cap = new DialCap();
        cap.setRadius(0.1);
        this.setCap(cap);
        
        // Neddle
        DialPointer needle = new DialPointer.Pointer();
        needle.setRadius(0.9);
        this.addLayer(needle);
        
        // Background
        this.setBackground(new DialBackground(Color.white));
        
        // Value printed on dial
        DialValueIndicator dvi = new DialValueIndicator(0);
        dvi.setFont(new Font("Arial", Font.BOLD, 40));
        dvi.setValueAnchor(RectangleAnchor.CENTER);
        dvi.setRadius(0.65);
        dvi.setBackgroundPaint(Color.white);
        dvi.setOutlinePaint(Color.white);
        dvi.setPaint(Color.black);
        dvi.setTextAnchor(TextAnchor.CENTER);
        dvi.setNumberFormat(new DecimalFormat("###"));
        this.addLayer(dvi);
	}
	
	@Override
	public void update(Observable obs, Object arg) {
		if (arg instanceof Number) {
			data.setValue((Number) arg);
		}
	}
}
